package dangs.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import dang.util.CommonConstants;
import dangs.vo.QnAVO;

public class QnADAO {
	private SqlMapClientTemplate client;
	private static QnADAO instance=new QnADAO();
	
	public QnADAO() {}
	public QnADAO(SqlMapClientTemplate client) {
		this.client=client;
	}
	public static QnADAO getInstance() {
		if(instance==null) {
			instance=new QnADAO();
		}
		return instance;
	}	
	public void insertContent(QnAVO qvo) throws SQLException {
		client.insert("insertContent", qvo);
	}
	public void updateContent(QnAVO qvo) throws SQLException {
		client.update("updateContent", qvo);
	}
	public void deleteContent(int qnaNo) throws SQLException {
		client.delete("deleteContent", qnaNo);
	}
	public QnAVO selectContent(int qnaNo) throws SQLException {
		return (QnAVO)client.queryForObject("selectContent", qnaNo);
	}
	public void updateViewCnt(int qnaNo) throws SQLException {
		client.update("updateViewCnt", qnaNo);
	}
	public void updateRestep(int qnaRef, int qnaRestep) throws SQLException {
		HashMap map=new HashMap();
		map.put("qnaRef", qnaRef);
		map.put("qnaRestep", qnaRestep);
		client.update("updateRestep", map);
	}
	public ArrayList<QnAVO> selectContentList(String productCode, int page) throws SQLException {
		System.out.println(productCode+" : "+page);
		int contentNo=CommonConstants.CONTENT_NUMBER_PER_PAGE;
		HashMap map=new HashMap();
		map.put("contentNo", contentNo);
		map.put("productCode", productCode);
		map.put("page", page);
		ArrayList<QnAVO> list = (ArrayList)client.queryForList("selectContentList", map);	
		return list;
	}
	public int selectTotalCount(String productCode) throws SQLException {
		int totalCnt=(Integer)(client.queryForObject("selectTotalCount", productCode));
		System.out.println(totalCnt);
		return totalCnt;
	}
	public int selectMaxNo(String productCode) throws SQLException {
		int maxNo=(Integer)client.queryForObject("selectMaxNo", productCode);
		return maxNo;
	}
	public int selectMaxRef() throws SQLException {
		int maxRef = (Integer) client.queryForObject("selectMaxRef");
		return maxRef;
	}
}
